import 'package:flutter/material.dart';
import '../tool/public_color.dart';
import '../tool/tool.dart';
import 'questionmodel.dart';

class QuestionOptionCell extends StatefulWidget {
  const QuestionOptionCell(
      {super.key,
      required this.model,
      required this.i,
      required this.callback});

  final Question model;
  final int i;
  final ValueChanged<bool> callback;

  @override
  State<QuestionOptionCell> createState() => _QuestionOptionCellState();
}

class _QuestionOptionCellState extends State<QuestionOptionCell> {
  @override
  Widget build(BuildContext context) {
    return buildOption2(widget.i);
  }

  Widget buildOption2(int i) {
    final optionModel = widget.model.option![i];
    final isShowImage = widget.model.option![i].img.isNotEmpty;
    return LayoutBuilder(builder: (context, constraints) {
      return GestureDetector(
        onTap: () {
          if (widget.model.questionStatus == QuestionStatus.check) {
            return;
          }

          if (widget.model.questionStatus == QuestionStatus.sureAnswer) {
            return;
          }

          if (widget.model.questionType == 2) {
            //多选
            optionModel.isSelected = !optionModel.isSelected;
            final selecteds = widget.model.option?.where((element) => element.isSelected) ?? [];

            if (selecteds.isNotEmpty) {
              widget.model.questionStatus = QuestionStatus.answerAndUnSure;
            }else {
              widget.model.questionStatus = QuestionStatus.unAnswer;
            }
            widget.callback(optionModel.isSelected);


          }else {
            //单选

            if (optionModel.isSelected) {
              // 避免多次重复点击
              return;
            }

            widget.model.option?.forEach((element) {
              element.isSelected = false;
            });
            optionModel.isSelected = true;
            widget.callback(optionModel.isSelected);

          }
        },
        child: Container(
          margin: const EdgeInsets.only(top: 10),
          width: constraints.maxWidth,
          child: isShowImage
              ? Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    buildOptionRow(i),
                    if (isShowImage)
                      Container(
                        padding: EdgeInsets.only(left: 40),
                        child: Image.network(
                          widget.model.option![i].img,
                          fit: BoxFit.fitWidth,
                          width: 200,
                          errorBuilder: (context, _, error) {
                            return Image.asset(
                              assetImage("icon-imageError"),
                              width: 200,
                              fit: BoxFit.fitWidth,
                            );
                          },
                        ),
                      )
                  ],
                )
              : buildOptionRow(i),
        ),
      );
    });
  }

  Row buildOptionRow(int i) {
    final model = widget.model.option![i];
    final textColor = model.isSelected ? Colors.white:color66;
    final iconBackColor = model.isSelected ? primaryColor:Colors.white;
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          width: 24,
          height: 24,
          margin: const EdgeInsets.only(right: 16),
          decoration: BoxDecoration(
              color: iconBackColor,
              shape: BoxShape.circle,
              border: Border.all(
                color: color66,
              )),

          child: Center(
              child: Text(
            model.icon,
            style: TextStyle(color: textColor, fontSize: 16),
          )),
        ),
        Expanded(
          child: Text(
            model.content,
            style: TextStyle(color: color33, fontSize: 16),
          ),
        ),
      ],
    );
  }
}
